package com.sysu.ooad.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.dto.StudentFacade;
import com.sysu.ooad.entity.Student;
import com.sysu.ooad.entity.Teacher;
import com.sysu.ooad.service.StudentService;
import com.sysu.ooad.service.TeacherService;
import com.sysu.ooad.util.AjaxCommand;

@Component
@Scope("prototype")
public class StudentManageAction extends CommonAction {
	private static final long serialVersionUID = 1L;
	private StudentService studentService;
	private TeacherService teacherService;
	private String name;
	private String password;
	private List<Student> studentList;
	private List<Teacher> assistantList;
	
	
	public TeacherService getTeacherService() {
		return teacherService;
	}
	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	public List<Teacher> getAssistantList() {
		return assistantList;
	}
	public void setAssistantList(List<Teacher> assistantList) {
		this.assistantList = assistantList;
	}
	public List<Student> getStudentList() {
		return studentList;
	}
	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
	public StudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String addStudent() throws Exception {
		Teacher t = (Teacher)session.get("teacher");
		Student s = studentService.addStudent(name, password, t.getCourse());
		HttpServletResponse response = ServletActionContext.getResponse();
		AjaxCommand.ajaxReturnJSON(response, new StudentFacade(s));
		return null;
	}
	
	public String execute() throws Exception {
		Teacher t = this.findCurrentTeacher();
		studentList = studentService.findByCourse(t.getCourse());
		assistantList = teacherService.findAssistantsByCourse(t.getCourse());
		System.out.println(studentList.size());
		return Action.SUCCESS;
	}
}
